package com.yzycoc.hamster.demo.time202505.time20250528.test;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.FillPatternType;

import java.util.ArrayList;
import java.util.List;

public class ColorExcelGenerator {

    public static void main(String[] args) {
        String fileName = "颜色代码列表.xlsx";

        // 1. 生成颜色数据
        List<ColorInfo> colorList = new ArrayList<>();
        for (IndexedColors color : IndexedColors.values()) {
            colorList.add(new ColorInfo(color.name(), color.getIndex()));
        }

        // 2. 生成Excel并写入
        EasyExcel.write(fileName, ColorInfo.class)
                .registerWriteHandler(new HorizontalCellStyleStrategy(null, generateContentStyles()))
                .sheet("颜色列表")
                .doWrite(colorList);

        System.out.println("Excel生成完成：" + fileName);
    }

    // 数据类
    public static class ColorInfo {
        @ExcelProperty("颜色名称")
        private String colorName;

        @ExcelProperty("颜色代码")
        private Short colorIndex;

        public ColorInfo(String colorName, short colorIndex) {
            this.colorName = colorName;
            this.colorIndex = colorIndex;
        }

        public String getColorName() {
            return colorName;
        }

        public Short getColorIndex() {
            return colorIndex;
        }
    }

    // 设置每个颜色对应的单元格背景色
    private static List<WriteCellStyle> generateContentStyles() {
        List<WriteCellStyle> styles = new ArrayList<>();
        for (IndexedColors color : IndexedColors.values()) {
            WriteCellStyle style = new WriteCellStyle();
            style.setFillForegroundColor(color.getIndex());
            style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

            WriteFont font = new WriteFont();
            font.setFontHeightInPoints((short) 10);
            font.setColor(IndexedColors.BLACK.getIndex());
            style.setWriteFont(font);

            styles.add(style);
        }
        return styles;
    }
}



